<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="filter-container filter">
      <el-form-item label="容器名称">
        <el-input v-model="listQuery.caskName" clearable class="filter-item" style="width: 200px;"
          placeholder="请输入容器名称" />
      </el-form-item>
      <div>
        <el-form-item>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.lookup, color: buttonColor.color }" @click="handleFilter">查找</el-button>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.add, color: buttonColor.color }" @click="handleCreate">添加</el-button>

        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.cancellation, color: buttonColor.color }" @click="cancellation">注销
        </el-button>
        <el-button round size="medium" class="filter-item"
          :style="{ background: buttonColor.reduction, color: buttonColor.color }" @click="reduction">还原</el-button>
      </el-form-item>
      </div>

    </el-form>

    <!-- 查询结果 -->
    <el-table :height="buttonColor.clientHeightTwo" v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。"
      border fit highlight-current-row :header-cell-style="tableColor" @selection-change="handleSelectionChange">
      <el-table-column align="center" :width="buttonColor.operationTwo" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{ color: buttonColor.edit, cursor: 'pointer' }" @click="handleUpdate(scope.row)">
              <img src="../../../public/images/edit.png" />
              编辑
            </div>
            <div :style="{ color: buttonColor.delete, cursor: 'pointer' }" @click="handleDelete(scope.row)">
              <img src="../../../public/images/del.png" />
              删除
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序" type="index" />
      <el-table-column align="left" label="容器名称" prop="caskName" />
      <el-table-column align="left" :formatter="containerTypeFormat" label="容器类型" prop="containerType" />
      <el-table-column align="left" label="容器皮重" prop="netWeight" />
      <el-table-column align="left" :width="buttonColor.state" label="状态" prop="caskStatus">
        <template slot-scope="scope">
          <span :style="{ color: buttonColor.statedisable }" v-if="scope.row.caskStatus === 0">{{
              scope.row.caskStatus | statusFilter
          }}</span>
          <span :style="{ color: buttonColor.statestart }" v-else>{{ scope.row.caskStatus | statusFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog width="auto" top="0vh" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="auto">
        <el-row>
          <el-col :span="12">
            <el-form-item class="required" label="容器名称" prop="caskName">
              <el-input v-model="dataForm.caskName" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item class="required" label="皮重(kg)" prop="netWeight">
              <el-input v-model="dataForm.netWeight" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="容器类别" prop="containerType">
              <!-- <el-input v-model="dataForm.containerType" /> -->
              <el-radio-group v-for="(item, index) in containerTypeList" :key="index" v-model="dataForm.containerType">
                <el-radio :label="item.attributeCode">{{ item.attributeValue }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item style="width: 100% !important" class="text" label="描述" prop="remark">
              <el-input v-model="dataForm.remark" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button round :style="{ background: buttonColor.cancel, color: buttonColor.colorone }"
          @click="dialogFormVisible = false">取消</el-button>
        <el-button round v-if="dialogStatus == 'create'"
          :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="createData">确定</el-button>
        <el-button round v-else :style="{ background: buttonColor.confirm, color: buttonColor.color }" @click="updateData">确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.button-margin {
  margin: 2px 0px;
}

.el-button+.el-button {
  margin-left: 1px;
}
</style>

<script>
import { listcaskCategory, createcaskCategory, updatecaskCategory, deletecaskCategory, setStatus } from '@/api/dangerousWaste/caskCategory'
import { listattributeValue } from '@/api/dangerousWaste/attributeValue'
import Pagination from '@/components/Pagination'
import { tableColor, titleColor, buttonColor } from '@/filters/index'
import moment from 'moment'

const statusMap = {
  0: '无效',
  1: '有效'
}

export default {
  name: 'WBaseCaskCategory',
  components: { Pagination },
  filters: {
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      multipleSelection: [], // 多选列表
      containerTypeList: [], // 容器类型列表
      tableColor,
      titleColor,
      buttonColor,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        caskName: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      dataForm: {
        caskName: undefined,
        caskId: undefined,
        caskCode: undefined,
        caskStatus: undefined,
        containerType: undefined,
        netWeight: undefined,
        remark: undefined,
        warringWeight: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        caskName: [
          { required: true, message: '容器名称不能为空' },
        ],
        netWeight: [
          { required: true, message: '容器皮重不能为空' },
        ],
      }
    }
  },
  created() {
    this.getList()
    // 容器类别
    listattributeValue({ itemId: -44 })
      .then(response => {
        console.log('response', response);
        this.containerTypeList = response.data.data.list
      })
      .catch(() => {
        this.containerTypeList = []
      })

  },
  methods: {
    // 注销
    cancellation() {
      if (this.multipleSelection.length > 0) {
        let temp = []
        this.multipleSelection.forEach((item) => {
          temp.push(item.caskId)
        })
        setStatus({ ids: temp, status: 0 })
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '注销成功'
            })
            this.list.forEach(items => {
              if (temp.includes(items.caskId)) {
                items.caskStatus = 0
              }
            })

          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      } else {
        this.$notify.error({
          title: '失败',
          message: '至少选择一条数据'
        })
      }
    },
    // 还原
    reduction() {
      if (this.multipleSelection.length > 0) {
        let temp = []
        this.multipleSelection.forEach((item) => {
          temp.push(item.caskId)
        })
        setStatus({ ids: temp, status: 1 })
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '还原成功'
            })
            this.list.forEach(items => {
              if (temp.includes(items.caskId)) {
                items.caskStatus = 1
              }
            })
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      } else {
        this.$notify.error({
          title: '失败',
          message: '至少选择一条数据'
        })
      }
    },
    // 多选事件
    handleSelectionChange(val) {
      console.log('val', val);
      this.multipleSelection = val;
    },
    // 容器类型
    containerTypeFormat(row, column) {
      if (this.containerTypeList.length > 0) {
        for (let i = 0; i < this.containerTypeList.length; i++) {
          if (row.containerType == this.containerTypeList[i].attributeCode) {
            return this.containerTypeList[i].attributeValue
          }
        }
        return ''
      } else {
        return ''
      }
    },
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listcaskCategory(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        caskName: undefined,
        caskId: undefined,
        caskCode: undefined,
        caskStatus: undefined,
        containerType: undefined,
        netWeight: undefined,
        remark: undefined,
        warringWeight: undefined,
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.containerType = this.containerTypeList[0].attributeCode
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.dataForm.caskStatus = 1
          createcaskCategory(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加base_cask_category 容器类型成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.dataForm.containerType = this.dataForm.containerType.toString()
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatecaskCategory(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.caskId === this.dataForm.caskId) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新容器类型成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除容器类型', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletecaskCategory(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除容器类型成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>
